<?php
session_start();
header("Content-type:text/html;charset=utf-8");
require_once 'Medoo.php';
require_once 'Contact.php';

/**
 * 后台管理注册管理员类 GET
 * 前端入参：
 *  username:  用户名
 *  psd:  密码
 *  phone:  手机
 */
$name = $_GET['username'];
$pwd = $_GET['pwd'];
$phone = $_GET['phone'];


if ($name == null || $pwd == null) {
    returnResult(-2);
    return;
}

if (!preg_match("/^1[34578]\d{9}$/", $phone)) {
    returnResult(-3);
    return;
}

insertUser();


function insertUser()
{
    global $database;
    global $name;
    global $pwd;
    global $phone;


    $database->insert('manage_user', [
        'user_name' => $name,
        'password' => md5($pwd),
        'phone' => $phone
    ]);

    returnResult(1);

}


/**
 * @param $resultType 返回结果
 */
function returnResult($resultType)
{
    $resultMsg = "";
    $resultCode = "";
    if ($resultType == 1) {
        $resultCode = 200;
        $resultMsg = '注册成功';
    } else {
        $resultCode = 1;
        $resultMsg = '注册失败';
    }
    if ($resultType == -2) {
        $resultCode = -2;
        $resultMsg = '不可为空';
    }

    if ($resultType == -3) {
        $resultCode = -3;
        $resultMsg = '手机号格式不正确';
    }

    $result = array(
        'code' => urlencode($resultCode),
        'msg' => urlencode($resultMsg),
        'data' => null
    );

    echo urldecode(json_encode($result));
}
